package com.exc.mongodb.dao;

import com.exc.mongodb.model.LgResumeData;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

@Component
public class ResumeDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    public void save(LgResumeData lgResumeData){
        mongoTemplate.save(lgResumeData);
    }


    public DeleteResult removeByName(String name){
        Query query = new Query(Criteria.where("name").is(name));
        return mongoTemplate.remove(query, LgResumeData.class);
    }

    public UpdateResult update(LgResumeData lgResumeData){
        Query query = new Query();//Criteria.where("id").is(lgResumeData.getId())
        Update update = new Update();
        update.set("name", lgResumeData.getName());
        update.set("salary", lgResumeData.getSalary());
        return mongoTemplate.updateFirst(query, update, LgResumeData.class);
    }

    public LgResumeData findByName(String name){
        Query query = new Query(Criteria.where("name").is(name));
        return mongoTemplate.findById(query, LgResumeData.class);
    }

}
